<style scoped>
.wraper {
    padding: 0 30px;
}

.search {
    padding-bottom: 20px;
}

.search span {
    color: #68777c;
    font-weight: bold;
    padding-right: 18px;
}

.search button {
    background-color: #00a8f7;
    color: #fff;
    margin-left: 8px;
}

.top {
    margin-bottom: 20px;
    border-top: 1px dashed #cccccc;
}

.detail {
    border-top: 1px dashed #cccccc;
}

.wraper .headline {
    position: relative;
    font-size: 12px;
    color: #303134;
    font-weight: bold;
    margin: 10px 0;
    line-height: 32px;
}

.wraper .add-agent {
    position: absolute;
    top: 0px;
    right: 30px;
    background-color: #348bf2;
    color: #fff;
}

.iconfont {
    font-size: 14px;
}

.red-font {
    color: #ff1f1f;
}

/* 分页 start */

.page {
    margin: 20px 0;
}
.ivu-page {
    padding-bottom: 150px;
} 

/* 分页 end */


</style>

<template>
    <div class="employee-manage">
        <product-title></product-title>
        <div class="wraper">
            <div class="search">
                <span>快速搜索</span>
                <Input v-model="searchForm.true_name" placeholder="请输入客户姓名" style="width: 140px"></Input>
                <Select v-model="searchForm.sex" style="width:120px" placeholder='请选择性别'>
                    <Option value="0" >全部</Option>
                    <Option value="1" >男</Option>
                    <Option value="2" >女</Option>
                </Select>
                <Select v-model="searchForm.age" style="width:120px" placeholder='请选择年龄段'>
                    <Option value="0" >全部</Option>
                    <Option value="1" >0-18岁</Option>
                    <Option value="2" >19-30岁</Option>
                    <Option value="3" >31-40岁</Option>
                    <Option value="4" >41-50岁</Option>
                    <Option value="5" >51-60岁</Option>
                    <Option value="6" >60岁以上</Option>
                </Select>
                <Select v-model="searchForm.birthMonth" style="width:120px" placeholder='请选择生日月'>
                    <Option value="0" >全部</Option>
                    <Option value="1" >1月</Option>
                    <Option value="2" >2月</Option>
                    <Option value="3" >3月</Option>
                    <Option value="4" >4月</Option>
                    <Option value="5" >5月</Option>
                    <Option value="6" >6月</Option>
                    <Option value="7" >7月</Option>
                    <Option value="8" >8月</Option>
                    <Option value="9" >9月</Option>
                    <Option value="10" >10月</Option>
                    <Option value="11" >11月</Option>
                    <Option value="12" >12月</Option>
                </Select>
                 <Select v-model="searchForm.type" style="width:140px" placeholder='请选择类型'>
                    <Option value="10" >全部</Option>
                    <Option v-for="item in typeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
                 <Select v-model="searchForm.rank" style="width:140px" placeholder='请选择级别'>
                    <Option value="0" >全部</Option>
                    <Option v-for="item in rankList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
                <Button @click='search'>查询</Button>
            </div>
            <div class="detail">
                <div class="headline">
                    <p>
                        <span class="red-font">提示</span>：
                    </p>
                    <Button class="add-agent" @click='toDetail()'>新增客户</Button>
                </div>
                <div class="clearfix">
                    <Table border :columns="columns_customer" :data="data_customer"></Table>
                    <Row class="page fr">
                        <Col span="24">
                        <Page :total="customerTotal" show-total show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
                        </Col>
                    </Row>
                </div>
            </div>
        </div>
        <Modal v-model="showAddModal" :mask-closable='closeable' width="600" title="添加客户" @on-ok="add" :loading="loading">
            <Row>
                <Col span="18" offset="1">
                <Form :model="customerInfo" :ref="customerInfo" :rules="ruleValidate" :label-width="120">
                    <Form-item label="姓名：" prop="true_name" required>
                        <Input v-model="customerInfo.true_name" placeholder="请输入姓名"></Input>
                    </Form-item>
                    <Form-item label="身份证号：" prop="id_card" required>
                         <Input v-model="customerInfo.id_card" placeholder="请输入身份证号"></Input>
                    </Form-item>
                    <Form-item label="手机号：" prop="mobile" required>
                        <Input v-model="customerInfo.mobile" placeholder="请输入手机号"></Input>
                    </Form-item>
                    <Form-item label="家庭地址：">
                        <Input v-model="customerInfo.home_address" placeholder="请输入家庭地址"></Input>
                    </Form-item>
                    <Form-item label="邮箱：">
                        <Input v-model="customerInfo.email" placeholder="请输入邮箱"></Input>
                    </Form-item>
                    <Form-item label="类型：">
                        <Select v-model="customerInfo.type" style="width:306px" placeholder='请选择类型'>
                            <Option v-for="item in typeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                        </Select>
                    </Form-item>
                    <Form-item label="级别：">
                         <Select v-model="customerInfo.rank" style="width:306px" placeholder='请选择级别'>
                            <Option v-for="item in rankList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                        </Select>
                    </Form-item>
                </Form>
                </Col>
            </Row>
        </Modal>
    </div>
</template>

<script>
import productTitle from '../../components/productTitle'
import { getCustomerList,createCustomer,delCustomer } from '../../api/customer';
export default {
    data() {
        return {
            closeable: false,
            loading: true,
            page: 1,
            pagesize: 10,
            searchForm: {
                true_name: '',
            },
            customerInfo: {
                true_name:'',
                id_card:'',
                mobile:'',
            },
            customerTotal:0,
            showAddModal: false,
            typeList: [
                {
                    value: '0',
                    label: '准客户'
                },
                {
                    value: '1',
                    label: '签单客户'
                },
            ],
            rankList: [
                {
                    value: '1',
                    label: '金卡'
                },
                {
                    value: '2',
                    label: '白金卡'
                },
                {
                    value: '3',
                    label: '铂金卡'
                },
                {
                    value: '4',
                    label: '钻石卡'
                },
                {
                    value: '5',
                    label: '尊钻卡'
                },
                {
                    value: '6',
                    label: '私钻卡'
                },
                {
                    value: '7',
                    label: '黑钻卡'
                },
            ],
            columns_customer: [
                {
                    title: '姓名',
                    key: 'true_name',
                    align: 'center',
                },
                {
                    title: '性别',
                    key: 'gender',
                    align: 'center',
                },
                {
                    title: '生日',
                    key: 'solar_birthday',
                    align: 'center',
                },
                {
                    title: '年龄',
                    key: 'age',
                    align: 'center',
                },
                {
                    title: '身份证号',
                    key: 'id_card',
                    align: 'center',
                },
                {
                    title: '手机号',
                    key: 'mobile',
                    align: 'center',
                },
                // {
                //     title: '代理人姓名',
                //     key: 'id_card',
                //     align: 'center',
                // },
                {
                    title: '操作',
                    key: 'action',
                    width: 150,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.toDetail(params.row.id)
                                    }
                                }
                            }, '完善信息'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.delConfirm(params.row.id)
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                },
            ],
            data_customer: [

            ],
            ruleValidate: {
                true_name: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '手机号不能为空', trigger: 'blur' },
                ],
                home_address: [
                    { required: true, message: '家庭地址不能为空', trigger: 'blur' }
                ],
            },
        }
    },
    components: {
        productTitle
    },
    created() {
        this.getList();
    },
    methods: {
        // 搜索
        search() {
            this.getList();
        },
        // 获取员工列表
        getList() {
            let _params = {
                'p': this.page,
                'pagesize': this.pagesize,
                'true_name': this.searchForm.true_name,
                'type': this.searchForm.type,
                'rank': this.searchForm.rank,
                'birthMonth':this.searchForm.birthMonth,
                'age':this.searchForm.age,
                'sex':this.searchForm.sex,
            }
            new Promise((resolve, reject) => {
                getCustomerList(_params).then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.data_customer = response.data.data.list;
                        this.customerTotal = response.data.data.total;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        getDetail(param) {
            new Promise((resolve, reject) => {
                getCustomerDetail(param).then(response => {
                    if (response.data.err == 0) {
                        this.customerDetail = response.data.data;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // 页码改变
        pageChange(page) {
            this.page = page
            this.getList()
        },
        // 每页条数改变
        pageSizeChange(pagesize) {
            this.pagesize = pagesize
            this.getList();
        },

        add() {
            this.$refs[this.customerInfo].validate((valid) => {
                if (valid) {
                    new Promise((resolve, reject) => {
                        createCustomer(this.customerInfo).then(response => {
                            if (response.data.err == 0) {
                                this.$Message.success(response.data.info);
                                this.showAddModal = false;
                                this.loading = true;
                                this.getList();
                            } else {
                                this.$Message.error(response.data.info);
                                setTimeout(() => {
                                    this.loading = false;
                                    this.$nextTick(() => {
                                        this.loading = true;
                                    });
                                }, 2000);
                            }
                            resolve();
                        }).catch(error => {
                            reject(error);
                        });
                    });
                } else {
                    // 通过loading来阻止modal隐藏，然后手动修改loading来充值确定按钮
                    setTimeout(() => {
                        this.loading = false;
                        this.$nextTick(() => {
                            this.loading = true;
                        });
                    }, 2000);
                }
            })
        },
         // 删除时弹出对话框
        delConfirm(id) {
            this.$Modal.confirm({
                title: '提示',
                content: '是否要删除该客户?',
                onOk: () => {
                    let _params = {
                        'id': id,
                        agent_uid:85
                    }
                    this.del(_params);
                },
            });
        },
        //删除操作
        del(param) {
            new Promise((resolve, reject) => {
                delCustomer(param).then(response => {
                    if (response.data.err == 0) {
                        this.$Message.success(response.data.info);
                        this.getList();
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //添加or编辑代理人，打开modal
        showAdd(index) {
            this.showAddModal = !this.showAddModal;
            if (index != -1) {
                // let params = {
                //     'employee_id': this.data_employee[index].id
                // }
                // this.getDetail(params);
                this.customerInfo.id = this.data_customer[index].id
                this.customerInfo.true_name = this.data_customer[index].true_name
                this.customerInfo.mobile = this.data_customer[index].mobile
                this.customerInfo.id_card = this.data_customer[index].id_card
            } else {
                this.customerInfo = {};
            }
        },
        toDetail(id) {
            if(id){
                this.$router.push({'name':'customer_update',params:{id:id}});
            }else{
                this.$router.push({'name':'customer_create'});
            }
            
        }
    }
}
</script>
